﻿﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using iHub.App_Code;

namespace iHub
{
    public partial class Form2 : Form
    {
        public static Form2 form = null;
        AssignmentManager assm = new AssignmentManager();
        public Form2(string id2)
        {
            InitializeComponent();
            int tempid = Convert.ToInt32(id2);
            this.FormClosing += new FormClosingEventHandler(Start_Closing);
            Assignment tempassignment = new Assignment();
            List<string> templist = new List<string>();

            try
            {
                tempassignment = assm.getAssignDetails(tempid);
                RequestingUnit.Text = tempassignment.Request;
                Address.Text = tempassignment.Address;
                City.Text = tempassignment.City;
                BatchNo.Text = tempassignment.Batch.ToString();
                Desc.Text = tempassignment.Assign;
                Assign.Text = Convert.ToString(tempid);
                startDate.Text = tempassignment.Startdate;
                endDate.Text = tempassignment.Enddate;
                CI.Text = tempassignment.Ci;
                Requestor.Text = tempassignment.Requestor;
                SubjectType.Text = tempassignment.Subjecttype;

                templist = tempassignment.Accountnames;
                templist.ForEach(delegate(string name)
                {
                    AccountNames.Items.Add(name);
                });

                List<string> a = new List<string>();
                a = assm.getRates();

                if (tempassignment.Status == "Pending")
                {
                    Status.Text = tempassignment.Status;
                    Status.Items.Add("Complete");
                    Status.Items.Add("New");
                    Status.Items.Add("Pending");
                }

                if (tempassignment.Status == "Complete")
                {
                    Status.Text = tempassignment.Status;
                    Status.Items.Add("Pending");
                    Status.Items.Add("New");
                    Status.Items.Add("Complete");
                }

                if (tempassignment.Status == "New")
                {
                    Status.Text = tempassignment.Status;
                    Status.Items.Add("Pending");
                    Status.Items.Add("Complete");
                    Status.Items.Add("New");
                }

                if (tempassignment.Status == "")
                {
                    Status.Text = "New";
                    Status.Items.Add("New");
                    Status.Items.Add("Pending");
                    Status.Items.Add("Complete");
                }
                String dash = "-";

                Type1.Text = tempassignment.Type;

                for (int i = 0; i < a.Count; i++)
                {
                    Type1.Items.Add(a[i]);
                }

                Edit.DataSource = assm.EmployeeAssign();
                if (tempassignment.Ename != "")
                {
                    Edit.SelectedItem = tempassignment.Ename += dash += tempassignment.Eid;
                }
                else
                {
                    Edit.SelectedIndex = -1;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {


        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                main.form.Enabled = true;
                main.form.assEditDisplay_Refresh();
            }
        }

        private void assAddConf_Click(object sender, EventArgs e)
        {
            List<string> names = new List<string>();
            StringValidator validator = new StringValidator();

            try
            {
                if (RequestingUnit.Text == "" || Address.Text == "" || City.Text == "" || BatchNo.Text == "" || startDate.Text == "" || endDate.Text == "" || CI.Text == "" || SubjectType.Text == "" || AccountNames.Items.Count <= 0)
                {
                    DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    if (validator.IsSafeString(RequestingUnit.Text) && validator.IsSafeString(Address.Text) && validator.IsSafeString(City.Text) && validator.IsSafeString(Desc.Text) && validator.IsSafeString(Assign.Text) && validator.IsSafeString(CI.Text) && validator.IsSafeString(Requestor.Text))
                    {
                        if (Convert.ToDateTime(startDate.Text) > Convert.ToDateTime(endDate.Text))
                        {
                            DialogResult msg = MessageBox.Show("Incorrect Date", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        else
                        {
                            Assignment n = new Assignment();

                            n.Request = RequestingUnit.Text;
                            n.Address = Address.Text;
                            n.City = City.Text;
                            n.Batch = BatchNo.Text;
                            n.Assign = Desc.Text;
                            n.AssignId = Convert.ToInt32(Assign.Text);
                            n.Startdate = startDate.Text;
                            n.Enddate = endDate.Text;
                            n.Ci = CI.Text;
                            n.Requestor = Requestor.Text;
                            n.Subjecttype = SubjectType.Text;

                            foreach (string name in AccountNames.Items)
                            {
                                names.Add(name);
                            }

                            n.Accountnames = names;

                            String item = Edit.Text;
                            String name2, id2;
                            string[] words1 = item.Split('-');
                            String type = Type1.Text;
                            string[] words2 = type.Split('-');
                            String t;

                            if (Edit.SelectedIndex != -1)
                            {
                                name2 = words1[0];
                                id2 = words1[1];

                                n.Eid = id2;
                                n.Ename = name2;
                            }
                            else
                            {
                                n.Eid = "";
                                n.Ename = "";
                            }

                            t = words2[0];

                            n.Status = Status.Text;
                            n.Type = t;

                            assm.EditAssign(n);
                            DialogResult msg1 = MessageBox.Show("Assignment has been Updated in the Database", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            main.form.assEditDisplay_Refresh();
                        }
                    }
                    else
                    {
                        MessageBox.Show("There are illegal characters present in the text", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Type1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        public Boolean addAccountName(string name)
        {
            Boolean allowed = true;

            foreach (string accname in AccountNames.Items)
            {
                if (accname == name)
                {
                    allowed = false;
                }
            }

            if (allowed)
            {
                AccountNames.Items.Add(name);
            }

            return allowed;
        }

        public Boolean checkmodifiedname(string name)
        {
            Boolean allowed = true;

            foreach (string accname in AccountNames.Items)
            {
                if (accname == name)
                {
                    allowed = false;
                }
            }

            return allowed;
        }

        public void editAccountName(string name, string old)
        {
            for (int i = 0; AccountNames.Items.Count > i; i++)
            {
                if (AccountNames.Items[i].Equals(old))
                {
                    AccountNames.Items[i] = name;
                }
            }
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void assAccDelete_Click(object sender, EventArgs e)
        {
            if (AccountNames.SelectedIndex != -1)
            {
                AccountNames.Items.Remove(AccountNames.SelectedItem);
            }
            else
            {
                MessageBox.Show("No name selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void assAccAdd_Click(object sender, EventArgs e)
        {
            form = this;

            this.Enabled = false;

            addAccountNameb addname = new addAccountNameb();
            addname.Show();
        }

        private void assAccModify_Click(object sender, EventArgs e)
        {
            if (AccountNames.SelectedIndex != -1)
            {
                if (AccountNames.Items.Count > 0)
                {
                    string oldname = AccountNames.SelectedItem.ToString();
                    form = this;

                    this.Enabled = false;

                    modifyAccountNameb editname = new modifyAccountNameb(oldname);
                    editname.Show();
                }
                else
                {
                    MessageBox.Show("There is no item in the list", "List Empty", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            else
            {
                MessageBox.Show("No name selected", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
}